package com.daafan.minilib.http;

import net.tsz.afinal.http.AjaxCallBack;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.Toast;

public class HttpHandler extends AjaxCallBack<String> {

	public static final int	MSG_LOADING	= 1;
	public static final int	MSG_START	= 2;
	public static final int	MSG_SUCCESS	= 3;
	public static final int	MSG_FAIL	= 4;

	private ProgressDialog	mProgressDialog;

	/** 在BaseActivity或者Application时，setContext */
	private static Context	mContext;

	public HttpHandler(Context context) {
		mContext = context;
	}

	public void progressDialog() {
		if (mContext != null) {
			String loadingMsg = getProgressMsg();
			mProgressDialog = ProgressDialog.show(mContext, null, loadingMsg, true, cancelable());
		}
	}

	public void cancelProgressDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			try {
				mProgressDialog.cancel();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static Context getContext() {
		return mContext;
	}

	@Override
	public void onFailure(Throwable t, int errorNo, String strMsg) {
		Toast.makeText(mContext, "网络错误", 0).show();
	}

	protected String getProgressMsg() {
		return null;
	}

	protected boolean cancelable() {
		return true;
	}

	protected void onFinally() {}

}
